package sige

import org.springframework.dao.DataIntegrityViolationException

class DespesaDiversaController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [despesaDiversaInstanceList: DespesaDiversa.list(params), despesaDiversaInstanceTotal: DespesaDiversa.count()]
    }

    def create() {
        if(!params."ordem.id"){
            return redirect(controller: "despesa") 
        }
        [despesaDiversaInstance: new DespesaDiversa(params)]
    }

    def save() {
        def despesaDiversaInstance = new DespesaDiversa(params)
        if (!despesaDiversaInstance.save(flush: true)) {
            render(view: "create", model: [despesaDiversaInstance: despesaDiversaInstance])
            return
        }
        DispesaDiversaService service = new DispesaDiversaService()
        if(!service.inserir(despesaDiversaInstance)){
            despesaDiversaInstance.delete(flush: true)
            render(view: "create", model: [despesaDiversaInstance: despesaDiversaInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'despesaDiversa.label', default: 'DespesaDiversa'), despesaDiversaInstance.id])
        redirect(action: "show", id: despesaDiversaInstance.id)
    }

    def show(Long id) {
        def despesaDiversaInstance = DespesaDiversa.get(id)
        if (!despesaDiversaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'despesaDiversa.label', default: 'DespesaDiversa'), id])
            redirect(action: "list")
            return
        }

        [despesaDiversaInstance: despesaDiversaInstance]
    }

    def edit(Long id) {
        def despesaDiversaInstance = DespesaDiversa.get(id)
        if (!despesaDiversaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'despesaDiversa.label', default: 'DespesaDiversa'), id])
            redirect(action: "list")
            return
        }

        [despesaDiversaInstance: despesaDiversaInstance]
    }

    def update(Long id, Long version) {
        def despesaDiversaInstance = DespesaDiversa.get(id)
        if (!despesaDiversaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'despesaDiversa.label', default: 'DespesaDiversa'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (despesaDiversaInstance.version > version) {
                despesaDiversaInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'despesaDiversa.label', default: 'DespesaDiversa')] as Object[],
                          "Another user has updated this DespesaDiversa while you were editing")
                render(view: "edit", model: [despesaDiversaInstance: despesaDiversaInstance])
                return
            }
        }

        despesaDiversaInstance.properties = params

        if (!despesaDiversaInstance.save(flush: true)) {
            render(view: "edit", model: [despesaDiversaInstance: despesaDiversaInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'despesaDiversa.label', default: 'DespesaDiversa'), despesaDiversaInstance.id])
        redirect(action: "show", id: despesaDiversaInstance.id)
    }
    
    def delete(Long id) {
        def despesaDiversaInstance = DespesaDiversa.get(id)
        if (!despesaDiversaInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'despesaDiversa.label', default: 'DespesaDiversa'), id])
            redirect(action: "list")
            return
        }

        try {
            despesaDiversaInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'despesaDiversa.label', default: 'DespesaDiversa'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'despesaDiversa.label', default: 'DespesaDiversa'), id])
            redirect(action: "show", id: id)
        }
    }
}
